﻿/**********************************弘石项目*******************************************
* 功能描述:登录及首页
* 作者:李兰祥
* 时间:2021‎年‎12‎月‎7‎日
* 
**********************************弘石项目*******************************************/
using HongStone.API.Helper;
using HongStone.Domain.AttendanceManagement;
using HongStone.Domain.SystemSetupModel.InstitutionSettingModel;
using HongStone.Ince;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 登录首页控制器
/// </summary>
namespace HongStone.API.Controllers
{
    [ApiExplorerSettings(GroupName = "Login")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class LoginController : ControllerBase
    {
        private readonly LoginInce _app;
        private readonly JwtHelpers _add;
        public LoginController(LoginInce app, JwtHelpers add)
        {
            _add = add;
            _app = app;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddLogin(Login model)
        {
            return Ok(_app.AddLogin(model));
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[AllowAnonymous]
        public IActionResult ValidateLogon(Login model)
        {
            var list = _app.ValidateLogon(model);
            if (list != null)
            {
                var page = _add.GenerateToken(model.LoginAccount);
                HttpContext.Response.Headers["token"] = page;
                HttpContext.Response.Headers["Access-Control-Expose-Headers"] = "page";
                return Ok(list);
            }
            else
            {
                return Ok(list);
            }
        }

        /// <summary>
        /// 找回密码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RetrievePassword(string LoginAccount, string LoginName)
        {
            return Ok(_app.RetrievePassword(LoginAccount, LoginName));
        }

        /// <summary>
        /// 显示个人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetLogin(int LoginId)
        {
            return Ok(_app.GetLogin(LoginId));
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="LoginId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult XiuGai(Login model)
        {
            return Ok(_app.XiuGai(model));
        }

        /// <summary>
        /// 锁屏
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetLoginName(string Name, string PassWork)
        {
            return Ok(_app.GetLoginName(Name, PassWork));
        }

        /// <summary>
        /// 显示消息列表详细信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMessageApproval(string NeiRong, int ZhuangTai)
        {
            var totalCount = 0;
            var list = _app.GetMessageApproval(NeiRong, ZhuangTai,out totalCount);

            return Ok(new { list, totalCount });
        }

        /// <summary>
        /// 邮箱验证找回密码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult A(string Email) 
        {
            return Ok(_app.XiuGai(Email));
        }

        [HttpGet]
        public IActionResult Main(string Email)
        {

            MailMessage mailMsg = new MailMessage();//实例化对象
            mailMsg.From = new MailAddress("1424857950@qq.com", "弘石房产");//源邮件地址和发件人
            mailMsg.To.Add(new MailAddress(Email));//收件人地址
            mailMsg.Subject = "找回密码";//发送邮件的标题
            StringBuilder sb = new StringBuilder();
            sb.Append("您的新密码为：admin123,");
            sb.Append("请注意查收");
            mailMsg.Body = sb.ToString();//发送邮件的内容
            //指定smtp服务地址（根据发件人邮箱指定对应SMTP服务器地址）
            SmtpClient client = new SmtpClient();//格式：smtp.126.com  smtp.164.com
            client.Host = "smtp.qq.com";
            //要用587端口
            client.Port = 587;//端口
            //加密
            client.EnableSsl = true;
            //通过用户名和密码验证发件人身份
            client.Credentials = new NetworkCredential("1424857950@qq.com", "dcawipqsvkteijic"); // 
            //发送邮件
            try
            {
                client.Send(mailMsg);
                return Ok(_app.XiuGai(Email));
            }
            catch (SmtpException)
            {
                throw;
            }
        }
    }
}
